//
//  RaffleTableController.m
//  XiangCaoJiaYuan
//
//  Created by tongj on 2017/8/24.
//  Copyright © 2017年 tongj. All rights reserved.
//

#import "RaffleTableController.h"
#import "RaffleCell.h"
#import "RaffleModel.h"
#import "ListCollectionViewController.h"
#import "UIButton+LT.h"
@interface RaffleTableController ()<MJRefreshBaseViewDelegate>
{
    MJRefreshHeaderView *_headerView;
    MJRefreshFooterView *_footerView;
    BOOL _isLoad;//是否处于网络请求状态
    NSInteger _pageIndex;

}
@property (nonatomic,strong)NSMutableArray *dataArray1;

@end

@implementation RaffleTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    _pageIndex = 0;
    _isLoad =NO;
    _dataArray1  = [[NSMutableArray alloc]init];
    _headerView=[MJRefreshHeaderView header];
    _headerView.scrollView=self.tableView;
    _headerView.delegate=self;
    //上拉加载更多的视图
    _footerView=[MJRefreshFooterView footer];
    _footerView.scrollView=self.tableView;
    _footerView.delegate=self;

    self.title = @"抽奖记录";
    [UIButton backButton:self.navigationItem Col:self];
    [self questData];
    [self.tableView registerNib:[UINib nibWithNibName:@"RaffleCell" bundle:nil] forCellReuseIdentifier:@"RaffleCell"];
}
-(void)questData{
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    NSString *uid = [defaults objectForKey:@"shop_guid"];
    NSString *url = [NSString stringWithFormat:@"%@prize/infos?shop_guid=%@&times=%ld",ServiceURL2,uid,_pageIndex];
    [YBRequestManager GET:url finished:^(NSData *data) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [NSMutableArray array];
        NSArray *str11 = dict[@"data"];

        for (NSDictionary *dict1 in str11)
        {
            [array addObject:dict1];
        }
        if (_pageIndex==0) {
            [_dataArray1 removeAllObjects];
        }
        NSArray *array2 = [RaffleModel arrayOfModelsFromDictionaries:array error:nil];
        [_dataArray1 addObjectsFromArray:array2];
        [self.tableView reloadData];
        _isLoad = NO;
        [_headerView endRefreshing];
        [_footerView endRefreshing];

    } failed:^(NSString *error) {

    }];

}
-(void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if (_isLoad) {
        return;
    }
    _isLoad = YES;
    //如果是下拉刷新
    
    
    if (refreshView==_headerView) {
        _pageIndex=0;
    }else if (refreshView==_footerView){
        _pageIndex++;
    }
    [self questData];

}
- (void)dealloc {
    [_headerView free];
    [_footerView free];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray1.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RaffleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RaffleCell"];
    RaffleModel *model = _dataArray1[indexPath.row];
    [cell setModel:model];

    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListCollectionViewController* ListVc = [[ListCollectionViewController alloc]init];
     RaffleModel *model = _dataArray1[indexPath.row];
    ListVc.paizeid = model.guid;
    ListVc.prize_num = model.prize_num;
    [self.navigationController pushViewController:ListVc animated:NO];
}
-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}



@end
